
package cn.seapon.talker.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.seapon.talker.R;
import cn.seapon.talker.infocarrier.MessageAssortment;

public class MessageListAdapter extends BaseAdapter {
	
    private Context context = null;
    private List<MessageAssortment> msgList;
    
    /**
     * @param mainActivity
     */
    public MessageListAdapter(Context context,List<MessageAssortment> data) {
        this.context = context;
        this.msgList = data;
    }

    @Override
    public int getCount() {
        return msgList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_message_list_item, parent, false);
            holder = new ViewHolder();
            holder.TV_NUM = (TextView) convertView.findViewById(R.id.num_message);
            holder.IV_ICON = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.TV_TITLE = (TextView)convertView.findViewById(R.id.tv_title);
            holder.TV_MSG = (TextView)convertView.findViewById(R.id.tv_msg);
            holder.TV_TIME = (TextView)convertView.findViewById(R.id.tv_time);
            convertView.setTag(holder);
        } else {// 有直接获得ViewHolder
            holder = (ViewHolder)convertView.getTag();
        }
//        Log.i("SwipeAdapter", "getView position="+position);
        
        MessageAssortment msg = msgList.get(position);
        
        holder.TV_TITLE.setText(msg.getTitle());
        holder.TV_MSG.setText(msg.getMsg());
        holder.TV_TIME.setText(msg.getTime());
        holder.IV_ICON.setImageResource(msg.getIcon_id());
        if (msg.getNum() > 99) {
        	holder.TV_NUM.setText("99");
		}else if (msg.getNum() <= 0) {
			holder.TV_NUM.setVisibility(View.GONE);
		}else{
			holder.TV_NUM.setText(""+msg.getNum());
		}
//        //设置监听函数
//        holder.RL_RIGHT.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mListener != null) {
//                    mListener.onRightItemClick(v, position);
//                }
//            }
//        });
        
        return convertView;
    }

    /**
     * 数据中间保存者
     * @author Asuron
     * Created on 2015年1月1日 下午8:32:52
     */
	private  static class ViewHolder {

        TextView TV_TITLE;
        TextView TV_MSG;
        TextView TV_TIME;
        ImageView IV_ICON;
        TextView TV_NUM;

    }
    
    
    
    
}
